package com.cloud.bc.controller.ajax.center;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcContacapplys;
import com.cloud.bc.model.entity.BcContacts;
import com.cloud.bc.model.entity.BcSingleContacts;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.request.LikeRequest;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.service.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/center/contact")
public class Ajax_ContactController {
    private final BcUsersService bcUsersService;
    //同意表
    private final BcContactsService bcContactsService;
    private final BcSingleContactsService bcSingleContactsService;
    //申请表
    private final BcContacapplysService bcContacapplysService;
    private final BcSinglesService bcSinglesService;

    /**
     * 申请联系方式
     */
    @GetMapping("apply_contact")
    public AjaxResp apply_contact(HttpServletRequest request, HttpServletResponse response,
                                  Integer reviewUserId) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);

        //查看同意表是否存在存在则直接返回
        BcContacts bcContacts = new BcContacts();
        bcContacts.setUserId(userInfo.getId());
        bcContacts.setTargetId(reviewUserId);
        int count = bcContactsService.count(bcContacts);
        if (count > 0) {
            //不为空则返回
            BcSingleContacts bcSingleContacts = new BcSingleContacts();
            bcSingleContacts.setUserId(reviewUserId);
            BcSingleContacts bcSingleContactsResult = bcSingleContactsService.selectOne(bcSingleContacts);
            if (null != bcSingleContactsResult) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("user_id", reviewUserId);
                if (!StrUtil.isEmpty(bcSingleContactsResult.getEmail())) {
                    jsonObject.put("email", bcSingleContactsResult.getEmail());
                }
                if (!StrUtil.isEmpty(bcSingleContactsResult.getPhone())) {
                    jsonObject.put("phone", bcSingleContactsResult.getPhone());
                }
                if (!StrUtil.isEmpty(bcSingleContactsResult.getWechatid())) {
                    jsonObject.put("wechat", bcSingleContactsResult.getWechatid());
                    if (!StrUtil.isEmpty(bcSingleContactsResult.getWechatid())) {
                        jsonObject.put("wechatId", bcSingleContactsResult.getWechatid());
                    }
                    if (!StrUtil.isEmpty(bcSingleContacts.getWechatQrcode())) {
                        jsonObject.put("wechat_qrcode", bcSingleContacts.getWechatQrcode());
                    }
                }
                if (!StrUtil.isEmpty(bcSingleContactsResult.getQq())) {
                    jsonObject.put("qq", bcSingleContactsResult.getQq());
                }
                ajaxResp.setObj(jsonObject.toString());
                return ajaxResp;
            }
        }
        //申请联系方式
        BcContacapplys bcContacapplys = new BcContacapplys();
        bcContacapplys.setUserId(userInfo.getId());
        bcContacapplys.setTargetId(reviewUserId);
        int countApply = bcContacapplysService.count(bcContacapplys);
        if (countApply > 0) {
            //已经申请了
            //TODO 这时可以增加VIP验证
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("已经申请，请耐心等待");
            return ajaxResp;
        }
        bcContacapplys.setPaytype("setmeal");
        bcContacapplysService.save(bcContacapplys);
        return ajaxResp;
    }

    @GetMapping("contact")
    public AjaxResp contact(HttpServletRequest request, HttpServletResponse response,
                            LikeRequest req) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
//        type 2 申请看我，3我申请看的
        AjaxResp ajaxResp = new AjaxResp();
        Page page = new Page();
        page.setCurrent(req.getPage());
        page.setSize(5);
        BcContacapplys bcContacapplys = new BcContacapplys();
        if (req.getType().equals(3)) {
            bcContacapplys.setUserId(userInfo.getId());
        } else if (req.getType().equals(2)) {
            bcContacapplys.setTargetId(userInfo.getId());
        }
        IPage<BcContacapplys> bcContacapplysIPage = bcContacapplysService.selectPage(page, bcContacapplys);
        List<BcContacapplys> records = bcContacapplysIPage.getRecords();
        records.forEach(item -> {
            if (req.getType().equals(2)) {
                item.setSingle(bcSinglesService.selectOne(new BcSingles().setUserId(item.getTargetId())));
            } else if (req.getType().equals(3)) {
                item.setSingle(bcSinglesService.selectOne(new BcSingles().setUserId(item.getUserId())));
            }
        });
        ajaxResp.setList(records);
        return ajaxResp;
    }

    /**
     * 同意
     */
    @GetMapping("agree")
    public AjaxResp agree(HttpServletRequest request, HttpServletResponse response,
                          Integer id) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        BcContacapplys bcContacapplys = new BcContacapplys();
        bcContacapplys.setId(id);
        bcContacapplys.setTargetId(userInfo.getId());
        bcContacapplys.setAgree(1);
        bcContacapplysService.updateById(bcContacapplys);
        //向关系表写入值
        BcContacapplys byId = bcContacapplysService.getById(id);
        BcContacts bcContacts = new BcContacts();
        //这是申请人的用户ID
        bcContacts.setUserId(byId.getUserId());
        //因为是当前用户被同意查看
        bcContacts.setTargetId(userInfo.getId());
        bcContactsService.save(bcContacts);
        return ajaxResp;
    }

    /**
     * 拒绝
     */
    @GetMapping("refuse")
    public AjaxResp refuse(HttpServletRequest request, HttpServletResponse response,
                           Integer id) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        BcContacapplys bcContacapplys = new BcContacapplys();
        bcContacapplys.setId(id);
        bcContacapplys.setTargetId(userInfo.getId());
        bcContacapplys.setAgree(2);
        bcContacapplysService.updateById(bcContacapplys);
        return ajaxResp;
    }

}
